# 背包槽位UI控制脚本 - 管理单个物品槽的显示和交互
extends Panel

# 节点引用
@onready var item_texture : Sprite2D = $CenterContainer/Panel/Sprite2D      # 物品图标
@onready var item_label : Label = $CenterContainer/Panel/Label            # 物品数量标签
@onready var tooltip : Panel = $TooltipPanel                              # 工具提示面板
@onready var tooltip_label : RichTextLabel = $TooltipPanel/TooltipLabel  # 工具提示文本
@onready var context_menu : PopupMenu = $ContextMenu                     # 右键菜单

# 数据存储
var current_slot : Slot = null  # 当前槽位数据

# 初始化
func _ready() -> void:
	tooltip.visible = false
	context_menu.visible = false
	context_menu.id_pressed.connect(_on_context_menu_item_selected)

# 更新槽位显示
func update(slots: Slot):
	current_slot = slots
	if !slots.item:  # 空槽位
		item_label.visible = false
		item_texture.visible = false
		mouse_filter = Control.MOUSE_FILTER_IGNORE
	else:  # 有物品
		mouse_filter = Control.MOUSE_FILTER_STOP
		item_label.visible = true
		item_label.text = str(slots.amount)
		item_texture.visible = true
		item_texture.texture = slots.item.texture

# 鼠标悬停显示工具提示
func _on_mouse_entered() -> void:
	if not current_slot or not current_slot.item:
		return
	
	# 构建提示内容：名称 + 类型 + 描述
	var item = current_slot.item
	var tooltip_content = item.name + "\n"
	if item.item_type:
		tooltip_content += item.item_type + "\n"
	if item.description:
		tooltip_content += item.description
	
	tooltip_label.text = tooltip_content
	tooltip.visible = true

# 鼠标离开隐藏工具提示
func _on_mouse_exited() -> void:
	tooltip.visible = false

# 右键点击处理
func _on_gui_input(event: InputEvent) -> void:
	if not current_slot or not current_slot.item:
		return
	
	# 检测右键点击
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
		_show_context_menu()
		accept_event()

# 菜单项选择处理
func _on_context_menu_item_selected(id: int):
	match id:
		0: _use_item()    # 使用物品
		1: _drop_item()    # 丢弃物品

# 使用物品
func _use_item():
	if not current_slot or not current_slot.item:
		return
	
	var player = get_tree().get_first_node_in_group("player")
	
	# 根据物品类型执行不同效果
	match current_slot.item.name:
		"蘑菇": player.heal()      # 蘑菇回复生命
		"火焰花": player.eat()      # 火焰花特殊效果
	
	# 减少数量或清空槽位
	if current_slot.amount > 1:
		current_slot.amount -= 1
	else:
		current_slot.item = null
		current_slot.amount = 0
	
	update(current_slot)

# 丢弃物品 - 在地上生成对应数量的物品
func _drop_item():
	if not current_slot or not current_slot.item or current_slot.amount <= 0:
		return
	
	var player = get_tree().get_first_node_in_group("player")
	if not player:
		print("无法找到玩家")
		return
	
	# 获取物品场景路径
	var scene_path = _get_item_scene(current_slot.item.name)
	if not scene_path:
		return
	
	# 加载场景
	var item_scene = load(scene_path)
	if not item_scene:
		return
	
	# 生成对应数量的物品
	for i in range(current_slot.amount):
		var dropped_item = item_scene.instantiate()
		# 随机偏移避免重叠
		var offset = Vector2(randf_range(-60, 60), randf_range(-60, 60))
		dropped_item.global_position = player.global_position + offset
		get_tree().current_scene.add_child(dropped_item)
	
	# 清空槽位
	current_slot.item = null
	current_slot.amount = 0
	update(current_slot)

# 物品名称到场景路径映射
func _get_item_scene(item_name: String) -> String:
	match item_name:
		"蘑菇": return "res://scene/mushroom.tscn"
		"火焰花": return "res://scene/flower.tscn"
		"食人花": return "res://scene/chomper.tscn"
		"火球": return "res://scene/fireball.tscn"
		_: return "res://scene/mushroom.tscn"  # 默认返回蘑菇场景

# 显示右键菜单
func _show_context_menu():
	if not current_slot or not current_slot.item:
		return
	
	context_menu.clear()
	context_menu.add_item("使用", 0)
	context_menu.add_item("丢弃", 1)
	
	# 在鼠标位置显示菜单
	var mouse_pos = get_viewport().get_mouse_position()
	context_menu.position = mouse_pos
	context_menu.popup()
